#include <linux/init.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kernel.h>

#include "virtual_device.h"

MODULE_AUTHOR("Arkins");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Test kernel module.");

static char *g_devpath = NULL;
static struct bdev_handle *g_bdev_handle = NULL;
static virtual_device_t *g_virtual_device = NULL;

module_param(g_devpath, charp, S_IRUGO);

static int get_blk_device(void)
{
    if (g_devpath == NULL) {
        printk(KERN_ERR "please input device path\n");
        return -1;
    }
    g_bdev_handle = bdev_open_by_path(g_devpath, BLK_OPEN_READ | BLK_OPEN_WRITE, NULL, NULL);
    if (g_bdev_handle == NULL) {
        printk(KERN_ERR "lookup bdev failed.\n");
		return -1;
    }
    printk(KERN_INFO "get device success\n");
    return 0;
}

static int __init init_test(void)
{
    if (get_blk_device() == -1) {
        return 0;
    }
    g_virtual_device = create_virtual_device(g_bdev_handle->bdev);
    printk(KERN_INFO "Start Test.\n");
    return 0;
}

static void __exit exit_test(void)
{
    delete_virtual_device(g_virtual_device);
    if (g_bdev_handle != NULL) {
        bdev_release(g_bdev_handle);
    }
    printk(KERN_INFO "End Test.\n");
}

module_init(init_test)
module_exit(exit_test)
